package org.dromara.creditcard.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.creditcard.domain.CreditCard;
import org.dromara.creditcard.domain.CreditInRecord;
import org.dromara.creditcard.domain.CreditOutRecord;
import org.dromara.creditcard.domain.vo.IndexDataVO;
import org.dromara.creditcard.service.*;
import org.dromara.system.domain.SysRole;
import org.dromara.system.domain.SysUser;
import org.dromara.system.service.ISysRoleService;
import org.dromara.system.service.ISysUserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;


/**
 * 数据统计控制器
 *
 * @author mr.zhou
 * @date 2021-03-13
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/credit/view")
public class DataViewController extends BaseController {

    private final ICreditCardService creditCardService;

    private final ISysRoleService sysRoleService;

    private final ICreditOutRecordService creditOutRecordService;

    private final ICreditInRecordService creditInRecordService;

    private final ICreateAgentService createAgentService;

    private final ISysUserService sysUserService;

    /**
     * 新增刷卡账单记录
     */
    @GetMapping("/index/data")
    public R<IndexDataVO> indexData() {
        IndexDataVO indexDataVO = new IndexDataVO();

        indexDataVO.setIsAgent(0);
        Integer agentId = null;
        LoginUser loginUser = LoginHelper.getLoginUser();
        SysRole sysRole = sysRoleService.selectRoleByUserId(loginUser.getUserId());
        if(sysRole != null){
            if(sysRole.getRoleKey().equals("agent") || sysRole.getRoleKey().equals("business")) {
                agentId = loginUser.getAgentId();
                indexDataVO.setIsAgent(1);
            }
        }

        //管理的卡片数量
        QueryWrapper<CreditCard> qw = new QueryWrapper<>();
        if(agentId != null){
            qw.eq("agent_id", agentId);
        }
        long cardCount = creditCardService.count(qw);
        indexDataVO.setCardCount(cardCount);

        //管理的代理数量
        long agentCount = createAgentService.count();
        indexDataVO.setAgentCount(agentCount);



        //管理的业务员数量
        QueryWrapper<SysUser> qwUser = new QueryWrapper<>();
        if(agentId != null){
            qw.eq("agent_id", agentId);
        }
        long businessCount = sysUserService.count(qwUser);
        indexDataVO.setBusinessCount(businessCount);

        //今日数据
        //今日总需出账金额

        indexDataVO.setTodayNeedOutMoney(new BigDecimal(0));
        indexDataVO.setTodayOutCount(0);
        QueryWrapper<CreditOutRecord> qwTodayNeedOutMoney = new QueryWrapper<>();
        qwTodayNeedOutMoney.select("ifnull(sum(bill_money),0) as billMoney, count(id) as outCount");
        if(agentId != null){
            qwTodayNeedOutMoney.eq("agent_id", agentId);
        }
        qwTodayNeedOutMoney.between("bill_out_day", DateUtils.getDateBeginTime(new Date(),0),
                                        DateUtils.getDateEndTime(new Date(),0));
        CreditOutRecord needOut = creditOutRecordService.getOne(qwTodayNeedOutMoney);
        if(needOut != null){
            indexDataVO.setTodayNeedOutMoney(needOut.getBillMoney());
            indexDataVO.setTodayOutCount(needOut.getOutCount());
        }

        //今日总需进账金额
        indexDataVO.setTodayNeedInMoney(new BigDecimal(0));
        indexDataVO.setTodayInCount(0);
        QueryWrapper<CreditInRecord> qwTodayNeedInMoney = new QueryWrapper<>();
        qwTodayNeedInMoney.select("ifnull(sum(repay_money),0) as repayMoney, count(id) as inCount");
        if(agentId != null){
            qwTodayNeedInMoney.eq("agent_id", agentId);
        }
        qwTodayNeedInMoney.between("bill_repay_day", DateUtils.getDateBeginTime(new Date(),0),
            DateUtils.getDateEndTime(new Date(),0));
        CreditInRecord needIn = creditInRecordService.getOne(qwTodayNeedInMoney);
        if(needIn != null){
            indexDataVO.setTodayNeedInMoney(needIn.getRepayMoney());
            indexDataVO.setTodayInCount(needIn.getInCount());
        }

        //今日已出账金额
        indexDataVO.setTodayOutMoney(new BigDecimal(0));
        //今日总利润
        indexDataVO.setTodayProfit(new BigDecimal(0));
        QueryWrapper<CreditOutRecord> qwTodayOutMoney = new QueryWrapper<>();
        qwTodayOutMoney.select("ifnull(sum(bill_money),0) as billMoney, ifnull(sum(profit),0) as profit");
        if(agentId != null){
            qwTodayOutMoney.eq("agent_id", agentId);
        }
        qwTodayOutMoney.eq("state", 1);
        qwTodayOutMoney.between("bill_out_day", DateUtils.getDateBeginTime(new Date(),0),
            DateUtils.getDateEndTime(new Date(),0));
        CreditOutRecord outMoney = creditOutRecordService.getOne(qwTodayOutMoney);
        if(outMoney != null){
            indexDataVO.setTodayOutMoney(outMoney.getBillMoney());
            //今日总利润
            indexDataVO.setTodayProfit(outMoney.getProfit());
        }

        //今日已进账金额
        indexDataVO.setTodayInMoney(new BigDecimal(0));
        QueryWrapper<CreditInRecord> qwTodayInMoney = new QueryWrapper<>();
        qwTodayInMoney.select("ifnull(sum(repay_money),0) as repayMoney");
        if(agentId != null){
            qwTodayInMoney.eq("agent_id", agentId);
        }
        qwTodayInMoney.eq("state", 1);
        qwTodayInMoney.between("bill_repay_day", DateUtils.getDateBeginTime(new Date(),0),
            DateUtils.getDateEndTime(new Date(),0));
        CreditInRecord inMoney = creditInRecordService.getOne(qwTodayInMoney);
        if(inMoney != null){
            indexDataVO.setTodayInMoney(inMoney.getRepayMoney());
        }



        //管理的信用卡的总额度
        indexDataVO.setTotalLimit(new BigDecimal(0));
        QueryWrapper<CreditCard> qwTotalLimit = new QueryWrapper<>();
        qwTotalLimit.select("ifnull(sum(total_limit),0) as totalLimit");
        if(agentId != null){
            qwTotalLimit.eq("agent_id", agentId);
        }
        qwTotalLimit.eq("state", 1);
        CreditCard totalLimit = creditCardService.getOne(qwTotalLimit);
        if(totalLimit != null){
            indexDataVO.setTotalLimit(totalLimit.getTotalLimit());
        }


        //累积总利润
        indexDataVO.setTotalProfit(new BigDecimal(0));
        QueryWrapper<CreditOutRecord> qwTotalProfit = new QueryWrapper<>();
        qwTotalProfit.select("ifnull(sum(profit),0) as profit");
        if(agentId != null){
            qwTotalProfit.eq("agent_id", agentId);
        }
        qwTotalProfit.eq("state", 1);
        CreditOutRecord totalProfit = creditOutRecordService.getOne(qwTotalProfit);
        if(totalProfit != null){
            indexDataVO.setTotalProfit(totalProfit.getProfit());
        }




        return R.ok(indexDataVO);
    }

}
